package com.example.medical.controller.admin;


import com.example.medical.entity.dto.GetListDto;
import com.example.medical.entity.pojo.DeviceType;
import com.example.medical.entity.pojo.StoreroomType;
import com.example.medical.service.StoreroomTypeService;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/storeroom_type")
public class AdminStoreroomTypeController {

    @Autowired
    private StoreroomTypeService storeroomTypeService;

    @PostMapping("/get_storeroom_type_list")
    public Result AdminGetStoreroomList(@RequestBody GetListDto dto){
        return storeroomTypeService.AdminGetStoreroomTypeList(dto);
    }

    @GetMapping("/get_storeroom_type_detail")
    public Result AdminGetStoreroomDetail(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(storeroomTypeService.getById(id));
    }

    @PostMapping("/save_storeroom_type")
    public Result AdminSaveStoreroomType(@RequestParam("name")String name){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        StoreroomType storeroomType=new StoreroomType();
        storeroomType.setStoreroomTypeName(name);
        storeroomTypeService.save(storeroomType);
        return Result.okResult(Code.SUCCESS);
    }

    @GetMapping("/remove_storeroom_type")
    public Result AdminRemoveStoreroomType(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        boolean checkRemove = storeroomTypeService.removeById(id);
        if (!checkRemove) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @GetMapping("/remove_storeroom_type_arr")
    public Result AdminRemoveStoreroomTypeArr(@RequestParam("ids")String[] ids){
        return storeroomTypeService.AdminRemoveStoreroomTypeArr(ids);
    }

    @GetMapping("/update_get_storeroom_type")
    public Result AdminUpdateGetStoreroom(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(storeroomTypeService.getById(id));
    }

    @PostMapping("/update_storeroom_type")
    public Result AdminUpdateStoreroomType(@RequestParam("id")Long id,
                                           @RequestParam("name")String name){
        return storeroomTypeService.AdminUpdateStoreroomType(id,name);
    }

}
